package com.example.sixteen.jt;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import java.util.ArrayList;
import java.util.List;

public class BoxDialog extends DialogFragment implements View.OnClickListener {
    private List<Integer> boxes;
    private OnBoxDialogListener listener;
    private EditText edit1, edit2, edit3, edit4, edit5, edit6, edit7, edit8, edit9, edit10, edit11, edit12, edit13, edit14, edit15, edit16;

    private BoxDialog(Builder builder) {
        this.boxes = builder.boxes;
        this.listener = builder.listener;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_box, null);
        edit1 = view.findViewById(R.id.edit_ant_box1);
        edit2 = view.findViewById(R.id.edit_ant_box2);
        edit3 = view.findViewById(R.id.edit_ant_box3);
        edit4 = view.findViewById(R.id.edit_ant_box4);
        edit5 = view.findViewById(R.id.edit_ant_box5);
        edit6 = view.findViewById(R.id.edit_ant_box6);
        edit7 = view.findViewById(R.id.edit_ant_box7);
        edit8 = view.findViewById(R.id.edit_ant_box8);
        edit9 = view.findViewById(R.id.edit_ant_box9);
        edit10 = view.findViewById(R.id.edit_ant_box10);
        edit11 = view.findViewById(R.id.edit_ant_box11);
        edit12 = view.findViewById(R.id.edit_ant_box12);
        edit13 = view.findViewById(R.id.edit_ant_box13);
        edit14 = view.findViewById(R.id.edit_ant_box14);
        edit15 = view.findViewById(R.id.edit_ant_box15);
        edit16 = view.findViewById(R.id.edit_ant_box16);
        if (boxes.size() == 16) {
            edit1.setText(String.valueOf(boxes.get(0)));
            edit2.setText(String.valueOf(boxes.get(1)));
            edit3.setText(String.valueOf(boxes.get(2)));
            edit4.setText(String.valueOf(boxes.get(3)));
            edit5.setText(String.valueOf(boxes.get(4)));
            edit6.setText(String.valueOf(boxes.get(5)));
            edit7.setText(String.valueOf(boxes.get(6)));
            edit8.setText(String.valueOf(boxes.get(7)));
            edit9.setText(String.valueOf(boxes.get(8)));
            edit10.setText(String.valueOf(boxes.get(9)));
            edit11.setText(String.valueOf(boxes.get(10)));
            edit12.setText(String.valueOf(boxes.get(11)));
            edit13.setText(String.valueOf(boxes.get(12)));
            edit14.setText(String.valueOf(boxes.get(13)));
            edit15.setText(String.valueOf(boxes.get(14)));
            edit16.setText(String.valueOf(boxes.get(15)));
        }
        view.findViewById(R.id.button_cancel).setOnClickListener(this);
        view.findViewById(R.id.button_confirm).setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        if (listener == null) return;

        if (view.getId() == R.id.button_cancel) listener.onCancel(this);
        else if (view.getId() == R.id.button_confirm) {
            List<Integer> newBoxes = new ArrayList<>();
            newBoxes.add(Integer.parseInt(edit1.getText().toString()));
            newBoxes.add(Integer.parseInt(edit2.getText().toString()));
            newBoxes.add(Integer.parseInt(edit3.getText().toString()));
            newBoxes.add(Integer.parseInt(edit4.getText().toString()));
            newBoxes.add(Integer.parseInt(edit5.getText().toString()));
            newBoxes.add(Integer.parseInt(edit6.getText().toString()));
            newBoxes.add(Integer.parseInt(edit7.getText().toString()));
            newBoxes.add(Integer.parseInt(edit8.getText().toString()));
            newBoxes.add(Integer.parseInt(edit9.getText().toString()));
            newBoxes.add(Integer.parseInt(edit10.getText().toString()));
            newBoxes.add(Integer.parseInt(edit11.getText().toString()));
            newBoxes.add(Integer.parseInt(edit12.getText().toString()));
            newBoxes.add(Integer.parseInt(edit13.getText().toString()));
            newBoxes.add(Integer.parseInt(edit14.getText().toString()));
            newBoxes.add(Integer.parseInt(edit15.getText().toString()));
            newBoxes.add(Integer.parseInt(edit16.getText().toString()));

            listener.onSetBox(this, newBoxes);
        }
    }

    public interface OnBoxDialogListener {
        void onCancel(BoxDialog dialog);

        void onSetBox(BoxDialog dialog, List<Integer> boxes);
    }

    public static class Builder {

        private List<Integer> boxes;
        private OnBoxDialogListener listener;

        public Builder() {

        }

        public Builder setBoxes(List<Integer> boxes) {
            this.boxes = new ArrayList<>(boxes);
            return this;
        }

        public Builder setListener(OnBoxDialogListener listener) {
            this.listener = listener;
            return this;
        }

        public Builder show(FragmentManager manager) {
            BoxDialog dialog = new BoxDialog(this);
            dialog.show(manager, "BoxDialog");
            return this;
        }
    }
}
